﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
#region Version Info
/* ========================================================================
* 【本页面功能概述】
*
* 作者：wangjianfeng 创建时间：2012/9/19 13:45:17
* 文件名：HttpUrlRewrite.cs
*
* 修改者： 时间：
* 修改说明：
* ========================================================================
*/
#endregion
namespace ChinaLeg.UrlRewrite
{
    public class HttpUrlRewrite : IHttpHandler
    {

        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {

            string url = context.Request.FilePath.ToLower();

            if (url.IndexOf("editor") > -1)
            {
                context.Response.WriteFile(context.Server.MapPath(url));
                return;
            }
            Regex reg = new Regex(@"/list/(list|case)(\d+).html[\?]?(.*)");
            if (reg.IsMatch(url))
            {
                string tempurl = context.Request.RawUrl;
                context.Server.Execute(reg.Replace(tempurl, "/list/$1.aspx?id=$2&$3"));
                return;
            }

            reg = new Regex(@"/news/(case|news|pictrue)(\d+).html");
            if (reg.IsMatch(url))
            {
                context.Server.Execute(reg.Replace(url, "/news/$1.aspx?id=$2"));
                return;
            }

            //reg = new Regex(@"/news/new(\d+).html");
            //if (reg.IsMatch(url))
            //{
            //    context.Server.Execute(reg.Replace(url, "/news/News.aspx?id=$1"));
            //    return;
            //}

            //reg = new Regex(@"/news/pic(\d+).html");
            //if (reg.IsMatch(url))
            //{
            //    context.Server.Execute(reg.Replace(url, "/news/CasePictrue.aspx?id=$1"));
            //    return;
            //}

            reg = new Regex(@"\.html", RegexOptions.IgnoreCase);
            if (reg.IsMatch(url))
            {
                context.Server.Execute(url.Replace(".html", ".aspx"));
                return;
            }
        }

        #endregion
    }
}
